"""
数据库连接对象并不是一个线程安全对象（不能被多个线程共享）
"""
from concurrent.futures.thread import ThreadPoolExecutor

import MySQLdb


def insert_data(start, end):
    conn = MySQLdb.connect(host='47.104.31.138', port=3306,
                           user='hellokitty', password='Hellokitty.618',
                           database='python2005', charset='utf8mb4')
    try:
        with conn.cursor() as cursor:
            for index in range(start, end + 1):
                cursor.execute(
                    'insert into tb_test1 (username, password) values (%s, %s)',
                    (f'user{index}', '000000')
                )
        conn.commit()
    finally:
        conn.close()


def main():
    with ThreadPoolExecutor(max_workers=100) as pool:
        start, end = 1, 1000
        for _ in range(100):
            pool.submit(insert_data, start, end)
            start, end = end + 1, end + 1000


if __name__ == '__main__':
    main()
